var express = require('express');
var router = express.Router();

var selectFields = '-password -token';

var createPassword = function(password) {
	return Util.mixPassword(password, 'waiter');
};

/* 获取 */
router.get('/', function (req, res, next) {
	var limit = req.query.limit;
	var skip = req.query.skip;

	var search = {};

	if (req.query.phone) {
		search.phone = new RegExp(req.query.phone, 'i');
	}
	if (req.query.sex) {
		search.sex = req.query.sex;
	}
	if (req.query.name) {
		search.name = new RegExp(req.query.name, 'i');
	}
	if (req.query.idNum) {
		search.idNum = new RegExp(req.query.idNum, 'i');
	}
	if (req.query.bankId) {
		search.bankId = new RegExp(req.query.bankId, 'i');
	}
	if (req.query.bankPerson) {
		search.bankPerson = new RegExp(req.query.bankPerson, 'i');
	}
	if (req.query.bankName) {
		search.bankName = new RegExp(req.query.bankName, 'i');
	}
	if (req.query.trades) {
		if (Array.isArray(req.query.trades)) {
			search.trades = {
				$in : req.query.trades
			};
		} else {
			search.trades = req.query.trades;
		}
	}
	if (req.query.hometown) {
		if (Array.isArray(req.query.hometown)) {
			search.idNum = {
				$in : req.query.hometown
			};
		} else {
			search.hometown = req.query.hometown;
		}
	}
	if (req.query.ageLevel) {
		if (Array.isArray(req.query.ageLevel)) {
			search.idNum = {
				$in : req.query.ageLevel
			};
		} else {
			search.ageLevel = req.query.ageLevel;
		}
	}
	if (req.query.empiricLevel) {
		if (Array.isArray(req.query.empiricLevel)) {
			search.idNum = {
				$in : req.query.empiricLevel
			};
		} else {
			search.empiricLevel = req.query.empiricLevel;
		}
	}
	if (req.query.eduLevel) {
		if (Array.isArray(req.query.eduLevel)) {
			search.idNum = {
				$in : req.query.eduLevel
			};
		} else {
			search.eduLevel = req.query.eduLevel;
		}
	}
	if (req.query.lng && req.query.lat) {
		search.loc = {
			$nearSphere: {
				$geometry: {
					type: 'point',
					'coordinates': [parseFloat(req.query.lng, 10), parseFloat(req.query.lat, 10)]
				},
				$maxDistance: 5000
			}
		};
	}
	console.log('waiter search : %j', search);

	Db.Waiter.count(search, function(err, count) {
		if (err) {
			res.send(500, err);
			return;
		}

		Db.Waiter.find(search, selectFields, {
			limit : limit,
			skip : skip
		}, function(err, waiters) {
			if (err) {
				res.send(500, err);
				return;
			}

			res.send({
				count : count,
				list : waiters
			});
		});
	});
});
router.get('/:id', function (req, res, next) {
	var id = req.params.id;
	Db.Waiter.findById(id, selectFields, function(err, waiter) {
		if (err) {
			res.send(500, err);
			return;
		}

		if (waiter) {
			res.send(waiter);
		} else {
			res.send(404);
		}
	});
});

/* 新增 */
router.post('/', function (req, res, next) {
	var waiter = new Db.Waiter(req.body);

	var err = waiter.validateSync();
	if (err) {
		res.send(500, err);
		return;
	}

	waiter.password = createPassword(waiter.password);

	waiter.save(function(err) {
		if (err) {
			res.send(500, err);
			return;
		}

		res.send(waiter);
	});
});

/* 更新位置 */
router.put('/loc', function(req, res, next) {
	console.log(req.cert);
	console.log(req.body);
	var id = req.cert.account._id;
	var lng = req.body.lng;
	var lat = req.body.lat;
	Db.Waiter.findById(id, function(err, waiter) {
		if (err) {
			res.send(500, err);
			return;
		}

		if (waiter) {
			waiter.set('loc', [
				lng, lat
			]);

			var err = waiter.validateSync();
			if (err) {
				res.send(500, err);
				return;
			}

			waiter.save(function(err) {
				if (err) {
					res.send(500, err);
					return;
				}

				res.send(waiter);
			});
		} else {
			res.send(404);
		}
	});
});

/* 修改 */
router.put('/:id', function(req, res, next) {
	var id = req.params.id;
	Db.Waiter.findById(id, function(err, waiter) {
		if (err) {
			res.send(500, err);
			return;
		}

		if (waiter) {
			var password = req.body.password;
			delete req.body.password;
			for (var path in req.body) {
				waiter.set(path, req.body[path]);
			}
			if (password) {
				waiter.password = createPassword(password);
			}

			var err = waiter.validateSync();
			if (err) {
				res.send(500, err);
				return;
			}

			waiter.save(function(err) {
				if (err) {
					res.send(500, err);
					return;
				}

				res.send(waiter);
			});
		} else {
			res.send(404);
		}
	});
});

/* 修改密码 */
router.put('/:id/password', function(req, res, next) {
	var id = req.params.id;
	Db.Waiter.findById(id, function(err, waiter) {
		if (err) {
			res.send(500, err);
			return;
		}

		if (waiter) {
			if (waiter.password != createPassword(waiter.oldPassword)) {
				waiter.invalidate('password', '原密码错误。', waiter.oldPassword);
				var err = waiter.validateSync();
				if (err) {
					res.send(400, err);
					return;
				}
			}

			waiter.password = req.body.password;

			var err = waiter.validateSync();
			if (err) {
				res.send(500, err);
				return;
			}

			waiter.password = createPassword(waiter.password);

			waiter.save(function(err) {
				if (err) {
					res.send(500, err);
					return;
				}

				res.send(waiter);
			});
		} else {
			res.send(404);
		}
	});
});


module.exports = router;
